package com.alibaba.datax.core.util.geo.amap;

import com.alibaba.datax.core.util.geo.MapGeo;
import com.alibaba.datax.core.util.geo.amap.dto.*;
import com.alibaba.datax.core.util.geo.response.GeoInfoResponse;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

public class AMapGeo implements MapGeo {
    private static final Logger LOG = LoggerFactory.getLogger(AMapGeo.class);
    private String key;
    private String version;

    public AMapGeo(String version, String key) {
        this.key = key;
        this.version = version;
    }

    /**
     * 地理编码
     *
     * @param address 结构化地址信息
     * @param city    指定查询的城市
     * @return location 坐标点 经度，纬度
     */
    @Override
    public GeoInfoResponse geo(String address, String city) {

        String geo = "https://restapi.amap.com/" + version + "/geocode/geo?key=" + key + "&address=" + address + "&city=" + city;

        String resStr = get(geo);
        if (resStr == null || resStr.trim().length() == 0) return null;

        GeoRootDTO geoRootDTO = JSONObject.parseObject(resStr, GeoRootDTO.class);

        return getGeoInfoResponse(geoRootDTO);
    }

    /**
     * 根据location定位街道信息
     *
     * @param location 坐标点 经度，纬度
     * @return
     */
    @Override
    public GeoInfoResponse regeo(String location) {
        // long startTs = System.currentTimeMillis();

        String resStr = get("https://restapi.amap.com/" + version + "/geocode/regeo?location=" + location + "&key=" + key);

        // long diff = System.currentTimeMillis() - startTs;
        // LOG.info("==============================================  :" + diff);

        if (resStr == null || resStr.trim().length() == 0) return null;
        ReGeoRootDTO resultInfo = JSONObject.parseObject(resStr, ReGeoRootDTO.class);
        return getReGeoResponse(resultInfo);
    }

    private GeoInfoResponse getReGeoResponse(ReGeoRootDTO resultInfo) {
        GeoInfoResponse response = GeoInfoResponse.builder().build();
        if (!Objects.isNull(resultInfo) && !Objects.isNull(resultInfo.getRegeocode())) {
            Regeocode regeocode = resultInfo.getRegeocode();
            AddressComponent addressComponent = regeocode.getAddressComponent();
            response.setStatus("成功");
            response.setInfocode(resultInfo.getInfocode());
            response.setProvince(addressComponent.getProvince());
            response.setCity(addressComponent.getCity());
            response.setDistrict(addressComponent.getDistrict());
            response.setFormatted_address(regeocode.getFormatted_address());
            response.setCityCode(addressComponent.getCitycode());
            return response;
        }
        return null;
    }

    /**
     * 处理响应体，转换
     *
     * @param geoRootDTO 参数实体
     * @return com.lucfzy.rest.dto.response.GeoInfoResponse
     * @Author liu-miss/lucfz
     * @Description 处理响应体，转换
     * @Date 2021/7/25 8:43
     **/
    private GeoInfoResponse getGeoInfoResponse(GeoRootDTO geoRootDTO) {
        GeoInfoResponse response = GeoInfoResponse.builder().build();
        if (!Objects.isNull(geoRootDTO) && !CollectionUtils.isEmpty(geoRootDTO.getGeocodes())) {
            List<Geocodes> geocodes = geoRootDTO.getGeocodes();
            if (CollectionUtils.isEmpty(geocodes)) {
                response.setStatus("失败");
                return response;
            }
            Geocodes apiReturn = geocodes.get(0);

            response.setStatus("成功");
            response.setInfocode(geoRootDTO.getInfocode());
            response.setProvince(apiReturn.getProvince());
            response.setCity(apiReturn.getCity());
            response.setDistrict(apiReturn.getDistrict());
            if (!CollectionUtils.isEmpty(apiReturn.getNeighborhood().getName())) {
                response.setCommunity(apiReturn.getNeighborhood().getName().get(0));
            }
            response.setFormatted_address(apiReturn.getFormatted_address());
            response.setCityCode(apiReturn.getCitycode());
            String location = apiReturn.getLocation();
            response.setLocation(location);
            return response;
        }
        return null;
    }
}


